package com.ciwong.epaper.common.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.ciwong.epaper.application.EApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * 被单词数据库
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String BASE_DB_NAME = "ciwong_wordbook";
    private static final int DB_VERSION = 1;
    private static DatabaseHelper instance = null;
    private static SQLiteDatabase db = null;
    private static long userId;


    private DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public static void initDatabase(Context context, long userId) {
        DatabaseHelper.userId = userId;
        instance = new DatabaseHelper(context, getDbName(), null, DB_VERSION);
    }

    private static synchronized DatabaseHelper getInstance() {
        if (instance == null) {
            instance = new DatabaseHelper(EApplication.getInstance(), getDbName(), null, DB_VERSION);
        }
        return instance;
    }

    public static synchronized SQLiteDatabase getDatabase() {
        if (db == null) {
            db = getInstance().getWritableDatabase();
        }
        return db;
    }

    public static synchronized void closeDatabase() {
        if (db != null) {
            db.close();
        }
    }

//    public static synchronized void dropDatabase() {
//        EApplication.getInstance().deleteDatabase()
//    }
    public static synchronized void clearDatabase() {
        List<String> tables = getTables();
        for (int i = 0; i < tables.size(); i++) {
            if ("sqlite_sequence".equals(tables.get(i)) || "android_metadata".equals(tables.get(i)))
                continue;
            if (tables.get(i).contains("word"))//单词数据库中标的命名都含有“word",加次判断防止多删有用表
                dropTable(tables.get(i));
        }
//        getTables();
    }

    private static String getDbName() {
        if (userId <= 0) {
            if (EApplication.getInstance() != null && EApplication.getInstance().getUserInfoBase() != null)
                userId = EApplication.getInstance().getUserInfoBase().getUserId();
        }
        String dbName = BASE_DB_NAME + "_" + userId;
        return dbName;
    }

    public static List<String> getTables() {
        List<String> tableNames = new ArrayList<>();
        String sql = "select name from sqlite_master where type='table' order by name ";
        try {
            Cursor cursor = getDatabase().rawQuery(sql, null);
            while (cursor.moveToNext()) {
                //遍历出表名
                String name = cursor.getString(0);
                tableNames.add(name);
                System.out.println("lzh db name : " + name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tableNames;
    }

    public static void dropTable(String tableName) {
        String sql = "DROP TABLE IF EXISTS " + tableName;
        getDatabase().execSQL(sql);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }


}
